import React, { Component } from "react";
import PropTypes from "prop-types";
import "./index.css";

export default class Item extends Component {
  static propTypes = {
    todo: PropTypes.object.isRequired,
    updateTodo: PropTypes.func.isRequired,
  };
  state = {
    mouse: false,
  };
  handleMouse = (isMouseIn) => {
    this.setState({ mouse: isMouseIn });
  };
  handleCheck = (todoId) => {
    return (e) => {
      this.props.updateTodo(todoId, e.target.checked);
    };
  };
  handleDelete = (id) => {
    return () => {
      this.props.deleteTodo(id);
    };
  };
  render() {
    const { todo } = this.props;
    const { mouse } = this.state;
    return (
      <li
        onMouseEnter={() => {
          this.handleMouse(true);
        }}
        onMouseLeave={() => {
          this.handleMouse(false);
        }}
        style={{ backgroundColor: mouse ? "#ddd" : "white" }}
      >
        <label>
          <input
            type="checkbox"
            checked={todo.isDone}
            onChange={this.handleCheck(todo.id)}
          />
          <span>{todo.name}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? "block" : "none" }}
          onClick={this.handleDelete(todo.id)}
        >
          删除
        </button>
      </li>
    );
  }
}
